<template>
  <el-form :model="item" ref="moForm" :rules="rules" label-width="130px">
    <el-form-item prop="cardName" label="运营商名称">
      <el-input
        v-model.trim="item.carrierName"
        placeholder="请输入运营商名称"
      ></el-input>
    </el-form-item>
    <el-form-item prop="price" label="价格">
      <el-input
        v-model.trim="item.price"
        placeholder="请输入价格"
      ></el-input>
    </el-form-item>
    <!-- 运营商主图  nameImg -->
    <el-form-item prop="carrierPictureFile" label="运营商主图">
      <el-button class="setimg" type="primary" @click="setImg('fileInput')"
        >选择图片</el-button
      >
      <br />
      <span class="imginfo">请选择.jpg/.png格式图片，大小不超过2M</span>
      <input
        type="file"
        accept=".jpg, .png"
        id="fileInput"
        @change="imgChange($event, 'carrierPictureFile')"
      />
      <div class="imgbox">
        <img :src="carrierPicture" id="image" />
      </div>
    </el-form-item>
    <!-- 资费详情图  centerImg -->
    <el-form-item prop="detailImageFile" label="资费详情图">
      <el-button class="setimg" type="primary" @click="setImg('fileInput1')"
        >选择图片</el-button
      >
      <br />
      <span class="imginfo">请选择.jpg/.png格式图片，大小不超过2M</span>
      <input
        type="file"
        accept=".jpg, .png"
        id="fileInput1"
        @change="imgChange($event, 'detailImageFile')"
      />
      <div class="imgbox">
        <img :src="detailImage" id="image" />
      </div>
    </el-form-item>
    <!-- 二维码图片  centerImg -->
    <el-form-item prop="qrcodeFile" label="二维码图片">
      <el-button class="setimg" type="primary" @click="setImg('fileInput2')"
        >选择图片</el-button
      >
      <br />
      <span class="imginfo">请选择.jpg/.png格式图片，大小不超过2M</span>
      <input
        type="file"
        accept=".jpg, .png"
        id="fileInput2"
        @change="imgChange($event, 'qrcodeFile')"
      />
      <div class="imgbox">
        <img :src="qrcode" id="image" />
      </div>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="handleSubmitClick()">提交</el-button>
      <el-button @click="handleBackClick">返回</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  props: ["item"],
  data() {
    const check_date = (rule, val, callback) => {
      console.log(this.item.beginTime && val);
      console.log(this.item.beginTime > val);
      if (this.item.beginTime && val && this.item.beginTime > val) {
        callback("请选择合法的开始结束时间");
      } else {
        callback();
      }
    };
    return {
      reader: null,
      imgBase: "",
      carrierPicture: "",
      detailImage: "",
      qrcode: "",
      cates: [],
      rules: {
        carrierPictureFile: [{ required: true, message: "请选择运营商图片", trigger: "blur" }],
        detailImageFile: [{ required: true, message: "请选择资费详情图片", trigger: "blur" }],
        qrcodeFile: [{ required: true, message: "请选择二维码图片", trigger: "blur" }],
      },
    };
  },
  computed: {
    obj() {
      return {
        carrierName: this.item.carrierName,
        price: this.item.price,
        carrierPictureFile: this.item.carrierPictureFile,
        carrierPicture: this.item.carrierPicture,
        detailImageFile: this.item.detailImageFile,
        detailImage: this.item.detailImage,
        qrcodeFile: this.item.qrcodeFile,
        qrcode: this.item.qrcode,
      };
    },
  },
  watch: {},
  created() {
    this.reader = new FileReader();
    this.reader.onload = (e) => {
      this.imgBase = e.target.result;
    };
    setTimeout(() => {
      if (this.item.carrierPicture)
        this.carrierPicture = this.item.carrierPicture;
      if (this.item.detailImage) this.detailImage = this.item.detailImage;
      if (this.item.qrcode) this.qrcode = this.item.qrcode;
    }, 500);
  },
  methods: {
    handleBackClick() {
      this.$router.go(-1);
    },
    handleSubmitClick() {
      this.$emit("doSubmit");
    },
    valid(callback) {
      this.$refs["moForm"].validate((valid) => {
        callback(valid);
      });
    },
    setImg(id) {
      const target = document.getElementById(id);
      target && target.click();
    },
    imgChange(e, image) {
      console.log(e);
      const img = e.target.files[0];
      if (img.size / 1024 < 2 * 1024) {
        this.reader.readAsDataURL(img);
        if (image === "carrierPictureFile") this.item.carrierPictureFile = img;
        if (image === "detailImageFile") this.item.detailImageFile = img;
        if (image === "qrcodeFile") this.item.qrcodeFile = img;
        setTimeout(() => {
          const pos = this.imgBase.indexOf("base64") + 7;
          this.imgBase = this.imgBase.substr(pos);
          if (image === "carrierPictureFile")
            this.carrierPicture = "data:image/png;base64," + this.imgBase;
          if (image === "detailImageFile")
            this.detailImage = "data:image/png;base64," + this.imgBase;
          if (image === "qrcodeFile")
            this.qrcode = "data:image/png;base64," + this.imgBase;
        }, 500);
      } else {
        this.$message.error("图片大小不可超过2M");
      }
      ["carrierPictureFile", "detailImageFile", "qrcodeFile"].forEach((i) => {
        this.$refs["moForm"].validateField(i);
      });
    },
  },
};
</script>

<style scoped>
#fileInput,
#fileInput1,
#fileInput2 {
  width: 0;
  height: 0;
  overflow: hidden;
}

.setimg {
  float: left;
  margin-left: 0;
}

.imgbox {
  border: 1px solid #eee;
  width: 200px;
  height: auto;
}

.imgbox img {
  align: center;
  max-width: 100%;
}

.imginfo {
  font-size: 12px;
  color: #666;
}
</style>
